package controllers

import (
	"fmt"
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"goravel/app/models"
)

type AuthController struct {
	// Dependent services
}

func NewAuthController() *AuthController {
	return &AuthController{
		// Inject services
	}
}

func (r *AuthController) Login(ctx http.Context) http.Response {
	var user models.User
	var err error
	err = facades.Orm().Query().Find(&user, 1)
	if err != nil {
		return ctx.Response().Json(http.StatusOK, http.Json{
			"message": err.Error(),
		})
	}
	fmt.Println(user.Name)
	var token string
	token, err = facades.Auth().Login(user)
	if err != nil {
		return ctx.Response().Json(http.StatusOK, http.Json{
			"message": err.Error(),
		})
	}
	return ctx.Response().Json(http.StatusOK, http.Json{
		"data": http.Json{
			"token": token,
		},
	})
}
